package br.com.pentagrama.service.ui.component;

import java.awt.Container;
import java.awt.Dimension;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;

import br.com.pentagrama.service.ui.event.IPChangedEvent;
import br.com.pentagrama.service.ui.event.IPRangeChangedEvent;

public class IPRangeField implements Field {

	private Field startField;
	private Field endField;
	private JLabel separator;
	
	private String oldStartIp;
	private String oldEndIp;
	private int ipFieldWidth;
	
	private JPanel panel;
	
	public IPRangeField(int width, int height, final IPRangeChangedEvent event) {
		panel = new JPanel();
		panel.setPreferredSize(new Dimension(width, height));
		panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));

		separator = new JLabel("-");
		oldStartIp = "";
		oldEndIp = "";
		
		this.startField = new IPField(ipFieldWidth, height, new IPChangedEvent() {
			
			public void updateValue(String value) {
				if (!value.equals(oldStartIp)) {
					oldStartIp = value;
					event.updateValue(value, oldEndIp);
				}
				
			}
		});
		this.endField = new IPField(ipFieldWidth, height, new IPChangedEvent() {
			
			public void updateValue(String value) {
				oldEndIp = value;
				event.updateValue(oldStartIp, value);
			}
		});
	}

	public void showIn(Container container) {
		startField.showIn(panel);
		panel.add(Box.createHorizontalStrut(25));
		panel.add(separator);
		panel.add(Box.createHorizontalStrut(25));
		endField.showIn(panel);		
		container.add(panel);
	}
	
}
